#!/usr/bin/env python

import os
import wx
import sys
import model
import widget
import resource as r

from __menuBar.__image import Post as ContextMenu

class Posts(widget.ListCtrl):
    """Posts list class
    
    Attributes:
        selectedModel: last selected model.
        selectedModels: all selected model.
        selectedIndexes: all selected index.
    """
    
    def __init__(self, parent):
        style = wx.LC_REPORT
        widget.ListCtrl.__init__(self, parent, style=style)
        self.InsertColumn(1, "No", wx.LIST_FORMAT_RIGHT)
        self.InsertColumn(2, "Tags")
        self.SetColumnWidth(0, 60)
        
        self.SetColumnWidth(1, wx.LIST_AUTOSIZE)
        
        self._images = wx.ImageList(16, 16)
        self._imagesIndex = {}
        
        index = self._images.Add(r.toolbar.Drive.Bitmap)
        self._imagesIndex[model.Post.STATUS_DOWNLOADED] = index
        index = self._images.Add(r.toolbar.WeatherCloud.Bitmap)
        self._imagesIndex[model.Post.STATUS_QUEUED] = index
        index = self._images.Add(r.toolbar.Disc.Bitmap)
        self._imagesIndex[model.Post.STATUS_ARCHIVED] = index
        index = self._images.Add(r.toolbar.CrossScript.Bitmap)
        self._imagesIndex[model.Post.STATUS_DELETED] = index
        
        self.SetImageList(self._images, wx.IMAGE_LIST_SMALL)
        
        self.menu = ContextMenu()
        
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self._selectModel)
        self.Bind(wx.EVT_LIST_ITEM_DESELECTED, self._deselectModel)
    
    _models = []
    
    @property
    def models(self):
        return self._models
    
    @models.setter
    def models(self, posts):
        self._models = posts
        self.Freeze()
        self.DeleteAllItems()
        for index, post in enumerate(posts):
            image = self._imagesIndex[self._getStatus(post)]
            no = format(index + 1, ',d')
            index = self.InsertImageStringItem(sys.maxint, no, image)
            self.SetStringItem(index, 1, post.tag.strip())
            self.SetItemData(index, post.id)
        self.Thaw()
        
    def _getStatus(self, post):
        """Return status of post.
        
        Because in viewer there will be post with downloaded status but the file
        is missing. Because sometimes we didn't carry all the image along. In
        that case we change the icon to Home icon. Meaning that the image is in
        home.
        
        Returns:
            A string of status. See Post status.
        """
        
        status = post.status
        if not post.file.exists:
            if status == post.STATUS_DOWNLOADED:
                status = post.STATUS_ARCHIVED
        return status
    
    def select(self, id):
        """Select item from list according to id.
        
        Args:
            id: An integer indicating the data of item list.
        """
        
        item = self.FindItemData(-1, id)
        if item == -1: return False
        
        state = wx.LIST_STATE_SELECTED | wx.LIST_STATE_FOCUSED
        self.SetItemState(item, state, state)
        self.SetItemImage(item, self._imagesIndex[model.Post.STATUS_DOWNLOADED])
        self.EnsureVisible(item)
        return True
    
    def _selectModel(self, event):
        self.selectedModel = self.models[event.Index]
        event.Skip()
    
    def _deselectModel(self, event):
        self.selectedModel = None
        event.Skip()
        
    @property
    def selectedModels(self):
        models = []
        for index in self.selectedIndexes:
            models.append(self.models[index])
        return models
    
    @property
    def selectedIndexes(self):
        indexes = []
        index = -1
        while True:
            t = (index, wx.LIST_NEXT_ALL, wx.LIST_STATE_SELECTED)
            index = self.GetNextItem(*t)
            if index == -1: break
            
            indexes.append(index)
            
        return indexes
    
    def delete(self):
        """Change icon to cross-script"""
        
        imageIndex = self._imagesIndex[model.Post.STATUS_DELETED]
        for index in self.selectedIndexes:
            self.SetItemImage(index, imageIndex)
        
    def queue(self):
        """Change icon to globe"""
        
        imageIndex = self._imagesIndex[model.Post.STATUS_QUEUED]
        for index in self.selectedIndexes:
            self.SetItemImage(index, imageIndex)
    
    def download(self):
        """change icon to drive"""
        
        for index in self.selectedIndexes:
            model = self.models[index]
            imageIndex = self._imagesIndex[self._getStatus(model)]
            self.SetItemImage(index, imageIndex)